import {Form, Input} from 'antd'
import {concat, omit} from 'ramda'
import {ReactNode} from 'react'
import Group from './Group'

const FormItem = Form.Item;
//matchValue格式
//{name,message,type}   //type:lg,lt,eq

const FormInput = ({initialValue, span, reg, textAlign, labelCol, prefix, subfix, label, justify,matchValue, getFieldValue, getFieldDecorator, name, required, disabled, placeholder, type, maxLength, min, len, max, rules, matchName}) => {

    //常用校验（特殊校验需要自己处理）

    //正则表达式
    const matchReg = (rule: object, value: string | number, callback: any) => {
        if (value && !reg.test.test(value)) {
            callback(reg.message || '数据格式不对');
        } else {
            callback();
        }
    }

    const maxCheck = (rule: object, value: string | number, callback: any) => {
        if (value && Number(getFieldValue && getFieldValue(name)) > (max || 0)) {
            callback('最大值为' + max);
        } else {
            callback();
        }
    }

    const minCheck = (rule: object, value: string | number, callback: any) => {
        if (value && Number(getFieldValue && getFieldValue(name)) < (min || 0)) {
            callback('最小值为' + min);
        } else {
            callback();
        }
    }

    const maxLenCheck = (rule: object, value: string | number, callback: any) => {
        if (value && (getFieldValue && (getFieldValue(name) || "").toString()).length > (maxLength || 0)) {
            callback('最大长度为' + maxLength);
        } else {
            callback();
        }
    }

    const lenCheck = (rule: object, value: string | number, callback: any) => {
        if (value && (getFieldValue && (getFieldValue(name) || '').toString()).length != len) {
            callback('最大值为' + max);
        } else {
            callback();
        }
    }

    const matchNameCheck = (rule: object, value: string | number, callback: any) => {

        if (value) {
            let flag = false;
            const mathV = matchName && getFieldValue && getFieldValue(matchName.name);
            if (matchName && matchName.type == 'lg' && mathV > value) {
                flag = true;
            } else if (matchName && matchName.type == 'lt' && mathV < value) {
                flag = true;
            } else if (mathV != value) {
                flag = true;
            }
            if (flag && matchName && matchName.message) {
                callback(matchName.message);
            } else {
                callback();
            }
        } else {
            callback();
        }
    }

    const matchValueCheck = (rule: object, value: string | number, callback: any) => {

        const mv = matchValue.value || '';
        const type = matchValue.type || '';

        if (value) {
            let flag = false;
            if (matchValue && type == 'lg' && mv > value) {
                flag = true;
            } else if (matchValue && type == 'lt' && mv < value) {
                flag = true;
            } else if (mv != value) {
                flag = true;
            }
            if(flag) {
                callback(matchValue && matchValue.message);
            }else {
                callback();
            }
        } else {
            callback(matchValue.message);
        }
    }


    let checkRules = [];
    if (required) {
        checkRules.push({required, message: '必填项'})
    }

    if (reg) {
        checkRules.push({
                validator: (rule: object, value: string | number, callback: any) => matchReg(rule, value, callback)
            }
        )
    }

    if (max) {
        checkRules.push({
                validator: (rule: object, value: string | number, callback: any) => maxCheck(rule, value, callback)
            }
        )
    }

    if (min) {
        checkRules.push({
                validator: (rule: object, value: string | number, callback: any) => minCheck(rule, value, callback)
            }
        )
    }

    if (maxLength) {
        checkRules.push({
                validator: (rule: object, value: string | number, callback: any) => maxLenCheck(rule, value, callback)
            }
        )
    }


    if (len) {
        checkRules.push({
                validator: (rule: object, value: string | number, callback: any) => lenCheck(rule, value, callback)
            }
        )
    }

    if (matchName) {
        checkRules.push({
                validator: (rule: object, value: string | number, callback: any) => matchNameCheck(rule, value, callback)
            }
        )
    }

    if (matchValue) {
        checkRules.push({
                validator: (rule: object, value: string | number, callback: any) => matchValueCheck(rule, value, callback)
            }
        )
    }

    return (
        <Group spans={[span || 16]} justify={justify || 'center'} textAligns={[textAlign || 'left']}>
            <FormItem
                labelCol={{span: labelCol || 0}}
                wrapperCol={{span: 24 - (labelCol || 0)}}
                label={label}
            >
                {getFieldDecorator(name, {
                    initialValue,
                    rules: concat(checkRules, rules || []),
                })(
                    <Input subfix={subfix}
                           autoComplete="off" prefix={prefix}
                           placeholder={placeholder || ''}
                           type={type || ''}
                           disabled={disabled}
                    />
                )}
            </FormItem>
        </Group>
    )
}

export default FormInput